/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IO;

/**
 *
 * @author Eagle
 */
public class calcClass implements Runnable {
    Thread t;
    int nTotalPre,nGluPre,nTotalSlot,gluConn,gabaConn,nTotalConn;
    int dgrp,tgrp,qgrp;
    groupResultClass result;

    public void run(){

        cellClass[] cell=new cellClass[nTotalPre];
        for (int i=0;i<nTotalPre;i++){
            cell[i]=new cellClass();
        }
//        cellClass cell2=new cellClass();
        //mark the positive ones

        int[] shuffleArray=new int[nTotalPre];
        for (int i=0;i<nTotalPre;i++)
        {
            shuffleArray[i]=i;
        }

        shuffleClass.shuffleArray(shuffleArray);


        for (int i=0;i<nGluPre;i++) {
            cell[shuffleArray[i]].setGlu();
        }


        int[] shuffleConnAry=new int[nTotalSlot];
        for (int i=0;i<nTotalSlot;i++)
        {
            shuffleConnAry[i]=i;
//            dp(nTotalSlot+" "+Integer.toString(i));
        }

        shuffleClass.shuffleArray(shuffleConnAry);

        int atShuffleAry=0,currGluConn=gluConn,currGABAConn=gabaConn;
        int currlink;

//        groupClass[] group=new groupClass[tgrp+qgrp];
        groupClass[] splitGroup=new groupClass[tgrp+qgrp*4];
        for (int c3=0;c3<splitGroup.length;c3++){
            splitGroup[c3]=new groupClass();
        }
        groupClass[] qGroup=new groupClass[qgrp];
        for (int c3=0;c3<qGroup.length;c3++){
            qGroup[c3]=new groupClass();
        }
        for (int i=0;i<nTotalConn;){
//            //dispatch all link;
            currlink=shuffleConnAry[atShuffleAry++];
            //test for positive pre, first find out pre cell sn
            linkInfoClass linkInfo=new linkInfoClass(currlink,dgrp,tgrp,qgrp);
            int currCell=linkInfo.pre();

            if (cell[currCell].isGlu()&&currGluConn>0) {

                
                if (linkInfo.groupId()>=tgrp+dgrp){
                    qGroup[linkInfo.groupId()-tgrp-dgrp].addGlu();
                }
                
                if (linkInfo.splitGrpId1()>0)
                splitGroup[linkInfo.splitGrpId1()].addGlu();
//                System.out.println(linkInfo.splitGrpId1()+",glu");

                if (linkInfo.splitGrpId2()>0){
                        splitGroup[linkInfo.splitGrpId2()].addGlu();
//                        System.out.println(linkInfo.splitGrpId2()+",glu");
                }
                currGluConn--;
                i++;

            }else if((!cell[currCell].isGlu())&&currGABAConn>0){

                if (linkInfo.groupId()>=tgrp+dgrp){
                    qGroup[linkInfo.groupId()-tgrp-dgrp].addGABA();

                }
                if (linkInfo.splitGrpId2()>0)
                splitGroup[linkInfo.splitGrpId1()].addGABA();
//                System.out.println(linkInfo.splitGrpId1()+",GABA");
                if (linkInfo.splitGrpId2()>0){
                        splitGroup[linkInfo.splitGrpId2()].addGABA();
//                        System.out.println(linkInfo.splitGrpId2()+",GABA");

                }
                currGABAConn--;
                i++;
            }

        } //all links are dispatched

        for (int c0=0;c0<qgrp;c0++){
            result.qaddGlu(qGroup[c0].gluConn());
            result.qaddGABA(qGroup[c0].GABAConn());
            result.qaddHyb(qGroup[c0].hybConn());
        }
        for (int c0=0;c0<tgrp+qgrp*4;c0++){
            result.splitAddGlu(splitGroup[c0].gluConn());
            result.splitAddGABA(splitGroup[c0].GABAConn());
            result.splitAddHyb(splitGroup[c0].hybConn());
        }


        
    }

    calcClass(groupResultClass Result, constClass c){
        nTotalPre=c.totalPre;
        nGluPre=c.gluPre;
        nTotalSlot=c.totalSlot;
        gluConn=c.gluConn;
        gabaConn=c.gabaConn;
        nTotalConn=c.totalConn;
        result=Result;
        dgrp=c.dgrp;
        tgrp=c.tgrp;
        qgrp=c.qgrp;

        t=new Thread(this, "Child Thread");
        t.start();
        //instance cells
    }
}
